package com.qqhf.hitfood.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qqhf.common.entity.AjaxResult;
import com.qqhf.hitfood.bean.dto.FoodTypeSalesDto;
import com.qqhf.hitfood.bean.entity.BizOrder;
import com.qqhf.hitfood.bean.entity.BizShop;
import com.qqhf.hitfood.bean.entity.TypeShop;
import com.qqhf.hitfood.bean.vo.FoodTypeSales;
import com.qqhf.hitfood.service.*;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Api(tags = "D - 首页图表")
@RestController
@RequestMapping("/dashboard")
public class DashboardController {
    @Autowired
    private IBizShopService bizShopService;
    @Autowired
    private ITypeShopService typeShopService;
    @Autowired
    private IAcctUserService acctUserService;
    @Autowired
    private IBizOrderService bizOrderService;
    @Autowired
    private ITypeFoodService typeFoodService;
    @Autowired
    private IBizOrderFoodService bizOrderFoodService;

    @Operation(summary = "获取店铺总数")
    @GetMapping("/nums")
    public AjaxResult nums() {
        HashMap<String, Object> result = new HashMap<>();
        int shopNum = bizShopService.list().size();
        result.put("shopNum", shopNum);
        int userNum = acctUserService.list().size();
        result.put("userNum", userNum);
        List<BizOrder> orders = bizOrderService.list();
        Double income = 0d;
        for (BizOrder order : orders) {
            income += order.getFeeTotal();
        }
        result.put("orderNum", orders.size());
        result.put("income", income);
        return AjaxResult.success(result);
    }

    @Operation(summary = "各食品种类销量变化图:7天")
    @GetMapping("/foodTypeSales")
    public AjaxResult foodTypeSales() {
        LocalDate today = LocalDate.now();
        List<String> xAxis = new ArrayList<>();
        List<Object> legend = typeFoodService.nameList();
        Map<String, Integer[]> map = new HashMap<>();
        for (Object o : legend) {
            Integer[] integers = {0, 0, 0, 0, 0, 0, 0};
            map.put(String.valueOf(o), integers);
        }
        for (int i = 7, j = 0; i > 0; i--, j++) {
            System.out.println("-------第 " + i + " 次-------");
            LocalDate curDay = today.plusDays(-i);
            xAxis.add(curDay.format(DateTimeFormatter.ofPattern("MM-dd")));
            List<FoodTypeSales> list = bizOrderFoodService.getFoodTypeSales(curDay + " 00:00:00", curDay + " 23:59:59");
            System.out.println("list:" + list);
            if (list != null) {
                for (FoodTypeSales sales : list) {
                    String name = sales.getName();
                    Integer[] integers = map.get(name);
                    integers[j] = sales.getNum();
                    map.put(name, integers);
                }
            }
        }
        List<FoodTypeSalesDto> series = new ArrayList<>();
        for (String s : map.keySet()) {
            FoodTypeSalesDto salesDto = new FoodTypeSalesDto();
            salesDto.setName(s);
            salesDto.setData(map.get(s));
            series.add(salesDto);
            Integer[] integers = map.get(s);
            System.out.print(s + "\t");
            for (Integer integer : integers) {
                System.out.print(integer + "\t");
            }
            System.out.println();
        }
        HashMap<String, Object> result = new HashMap<>();
        result.put("xAxis", xAxis);
        result.put("legend", legend);
        result.put("series", series);
        return AjaxResult.success(result);
    }

    @Operation(summary = "总销量变化图:7天")
    @GetMapping("/OrderChange")
    public AjaxResult OrderChange() {
        LocalDate today = LocalDate.now();
        List<String> xAxis = new ArrayList<>();
        List<Object> series = new ArrayList<>();
        for (int i = 7; i > 0; i--) {
            LocalDate curDay = today.plusDays(-i);
            xAxis.add(curDay.format(DateTimeFormatter.ofPattern("MM-dd")));
            List<Map<String, Object>> list = bizOrderService.selectList(new QueryWrapper<BizOrder>()
                    .select("sum(fee_total) as sum")
                    .between("create_time", curDay + " 00:00:00", curDay + " 23:59:59"));
            if (list != null && list.size() > 0 && list.get(0) != null) {
                series.add(list.get(0).get("sum"));
            } else {
                series.add(0);
            }

        }
        HashMap<String, Object> result = new HashMap<>();
        result.put("xAxis", xAxis);
        result.put("series", series);
        return AjaxResult.success(result);
    }

    @Operation(summary = "店铺分类分布:{分类名:数量}")
    @GetMapping("/shopDistribute")
    public AjaxResult shopDistribute() {
        List<Map<String, Object>> list = bizShopService.selectList(new QueryWrapper<BizShop>()
                .select("count(*) as value", "type_id")
                .groupBy("type_id"));
        List<Map<String, Object>> series = new LinkedList<>();
        List<String> legend = new ArrayList<>();
        for (Map<String, Object> map : list) {
            HashMap<String, Object> map1 = new HashMap<>();
            TypeShop typeShop = typeShopService.getById((Serializable) map.get("type_id"));
            map1.put("name", typeShop.getIntro());
            legend.add(typeShop.getIntro());
            map1.put("value", map.get("value"));
            series.add(map1);
        }
        HashMap<String, Object> result = new HashMap<>();
        result.put("legend", legend);
        result.put("series", series);
        return AjaxResult.success(result);
    }


}
